package com.hnxxxy.sysmgr.service.impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.sysmgr.mapper.ISysLogMapper;
import com.hnxxxy.sysmgr.pojo.Log;
import com.hnxxxy.sysmgr.pojo.Staff;
import com.hnxxxy.sysmgr.service.SysLogSercice;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Slf4j
public class SysLogSerciceImpl implements SysLogSercice {

    @Autowired
    ISysLogMapper logMapper;
    @Override
    public int insertSysLog(Log log){
        Subject subject = SecurityUtils.getSubject();
        Staff staff = (Staff) subject.getPrincipal();
        log.setUserId(staff.getUserid());
        return logMapper.insertSysLog(log);
    }

    @Override
    public List<Log> findAlSysLog(Log log) {
        return logMapper.findAlSysLog(log);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteSysLog(Integer[] ids) {
        try {
            for (int i = 0; i < ids.length; i++) {
                logMapper.deleteSysLog(ids[i]);
            }
            return AjaxRtnEntity.success;
        } catch (Exception e) {
            log.info("-->系统日志删除异常：" + e.toString());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxRtnEntity.error;
        }
    }


}
